package com.atguigu.spzx.cloud.user.controller;

import com.atguigu.spzx.cloud.model.entity.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Map;

//测试传参
@RestController
@RequestMapping("/user/param")
public class ParamController {

    //1、请求参数：通过变量入参   url?k=v&k2=v2
    @GetMapping("/test1")
    public String test1(String userName , String address){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        System.out.println(requestAttributes.getRequest().getHeader("company"));
        System.out.println("test1 , userName: "+ userName+" , address: "+ address);
        return "SUCCESS";
    }
    //2、请求参数： pojo入参
    @GetMapping("/test2")
    public String test2(User user){
        System.out.println("test2 , userName: "+ user.getUserName()+" , address: "+ user.getAddress());
        return "SUCCESS";
    }
    //3、请求参数：通过集合接收  Map     List
    @GetMapping("/test3")
    // @RequestParam 表示获取请求参数存到Map中
    public String test3(@RequestParam Map map){ //springmvc的接口中 Map和Model一样 代表需要存到request域
        System.out.println("test3 , userName: "+ map.get("userName")+" , address: "+ map.get("address"));
        return "SUCCESS";
    }
    //url?ids=1,2,3,4
    @GetMapping("/test4")
    public String test4(@RequestParam List<Integer> ids){//必须加注解 表示获取请求参数ids的值存到List集合中
        System.out.println("test4 , ids: "+ ids);
        return "SUCCESS";
    }
    //4、请求体参数：
    @PostMapping("/test5")
    public String test5(@RequestBody User user){
        System.out.println("test5 , user: "+ user);
        return "SUCCESS";
    }



    //5、路径参数： UserController中已经测试过
}
